package com.stylefeng.guns.core.common.Tools;

import com.stylefeng.guns.core.base.tips.Tip;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author zhangyalin
 * @Description //通用返回结果集
 * @Date 2018-09-07
 **/
@Data
@ApiModel(value="返回结果集",description = "系统统一返回结果集")
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    private String message;
    private int code;
    private T data;
    private Tip tip;
    private BizExceptionEnum bizExceptionEnum;
    private Result(T data) {
        this.code = 200;
        this.message = "成功";
        this.data = data;
    }


    private Result(CodeMsg cm) {
        if(cm == null){
            return;
        }
        this.code = cm.getRetCode();
        this.message = cm.getMessage();
    }
    private Result(BizExceptionEnum bizExceptionEnum) {
        if(bizExceptionEnum == null){
            return;
        }
        this.code = bizExceptionEnum.getCode();
        this.message = bizExceptionEnum.getMessage();
    }

    private Result(Tip tip) {
        if(tip == null){
            return;
        }
        this.code = tip.getCode();
        this.message = tip.getMessage();
    }
    /**
     * 成功时候的调用
     * @return
     */
    public static <T> Result<T> success(T data){
        return new Result<T>(data);
    }
    /**
     * 成功，不需要传入参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> Result<T> success(){
        return (Result<T>) success("");
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static <T> Result<T> error(CodeMsg cm){
        return new Result<T>(cm);
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static <T> Result<T> error(BizExceptionEnum bizExceptionEnum){
        return new Result<T>(bizExceptionEnum);
    }

    /**
     * 失败时候的调用(传递Tip)
     * @return
     */
    public static <T> Result<T> error(Tip tip){
        return new Result<T>(tip);
    }
    /**
     * 失败时候的调用,扩展消息参数
     * @param cm
     * @param msg
     * @return
     */
    public static <T> Result<T> error(CodeMsg cm,String msg){
        CodeMsg cms=new CodeMsg();
        cms.setMessage(cm.getMessage()+"--"+msg);
        cms.setRetCode(cm.getRetCode());
        return new Result<T>(cms);
    }


    public T getData() {
        return data;
    }
    public String getMessage() {
        return message;
    }
    public int getCode() {
        return code;
    }
}
